package com.zzl.gulimall.product.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.Locale;

/**
 * Title: ConvertMessage.class<br>
 * Description: <br>
 * @author zzl
 */
@Service
public class ConvertMessage {

    @Autowired
    private MessageSource messageSource;

    /**
     * 获取key对应的值
     *
     * @param key
     * @return
     */
    public String getMessage(String key) {
        Locale locale = LocaleContextHolder.getLocale();
        String message;
        try{
            message = messageSource.getMessage(key, null, locale);
        } catch (Exception e) {
            message = key;
        }
        return message;
    }

    /**
     * 获取当前语言
     *
     * @return
     */
    public String getLang() {
        Locale local = LocaleContextHolder.getLocale();
        StringBuilder currentUrl = new StringBuilder();
        currentUrl.append(local.getLanguage());
        currentUrl.append("_");
        currentUrl.append(local.getCountry());
        return currentUrl.toString();
    }

    /**
     * 当前环境是否是英文
     *
     * @return
     */
    public boolean isEnglish() {
        String lang = getLang();
        return String.valueOf(Locale.US).equals(lang);
    }

}
